package Demo3;

import java.util.ArrayList;

public class Solution2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @param m int整型 
     * @return int整型
     */
    public int LastRemaining_Solution (int n, int m) {
        // write code here
        ArrayList<Integer> cur = new ArrayList<>();

        for(int i = 0; i < n; i++) {
            cur.add(i);
        }
        int index = -1;
        while(cur.size() > 1) {
            int tmp = m;
            while(tmp != 0) {
             tmp--;
             index++;
             if(index == cur.size()) index = 0;
            }
            cur.remove(index);
            if(index == 0) {
                index = cur.size() - 1;
                continue;
            }
            index--;
        }
        return cur.remove(0);
    }
}
class Demo {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int ret = solution2.LastRemaining_Solution(5, 3);
        System.out.println(ret);
    }
}
